package org.blade.personal.utils.pdm.concre;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.blade.personal.utils.pdm.Pdm;
import org.blade.personal.utils.pdm.PdmColumn;
import org.blade.personal.utils.pdm.PdmKey;
import org.blade.personal.utils.pdm.PdmParser;
import org.blade.personal.utils.pdm.PdmTable;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class SimplePdmParser implements PdmParser{

	@Override
	public Pdm parseXml2Pdm(String pdmXml) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pdm parseXml2Pdm(Reader reader) {
		char[] charBuff = new char[1024];
		StringBuilder string =new StringBuilder();
		try {
			while( reader.read(charBuff) != -1 ){
				string.append(charBuff);
			}
			charBuff = new char[1024]; 
			reader.read(charBuff);
			string.append(charBuff);
			System.out.println(string);
			Document doc = DocumentHelper.parseText(string.toString());
			
			return getFromDocuemnt( doc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Pdm parseXml2Pdm(InputStream in){
		Reader reader = new InputStreamReader(in);
		try {
			int available = in.available();
			byte[] buff = new byte[available];
			StringBuilder pdm = new StringBuilder();
			while(-1 != in.read(buff)){
				pdm.append(new String(buff,"UTF-8"));
			}
//			System.out.println(pdm.toString());
			Document doc = DocumentHelper.parseText(pdm.toString());
			return getFromDocuemnt( doc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Pdm getFromDocuemnt(Document doc) {
		
		Pdm pdm = new Pdm(1);
		Element root = doc.getRootElement();
		List<Element> tables = (List<Element>)root.selectNodes("//c:Tables/o:Table");
		List<PdmTable> pdmTables = new ArrayList();
		for(Element elem : tables){
			pdmTables.add(getTable( elem));
		}
		
		pdm.setTables(pdmTables);
		return pdm;
	}
	
	private String getText(Element elem){
		if( null == elem){
			return null;
		}
		return elem.getText();
	}
	
	private Integer getInt(Element elem){
		
		if( null == elem || "".equals(elem.getText())){
			return null;
		}
		return Integer.parseInt(elem.getText());
	}
	
	private Long getLong(Element elem){
		if( null == elem || "".equals(elem.getText())){
			return null;
		}
		return Long.parseLong(elem.getText());
	}
	
	
	public PdmColumn getColumn(Element table){
		table.selectNodes("");
		
		

		
		
		return null;
	}
	
	@Override
	public List<PdmColumn> getColumns(Element tableElement) {
		// TODO Auto-generated method stub
		
		List<Element> columnsElem = (List<Element>)tableElement.selectNodes("./c:Columns/o:Column");
		String talbeId = tableElement.selectSingleNode("./a:ObjectID").getText();
		List<PdmColumn> columns = new ArrayList<PdmColumn>();
		for(Element elem : columnsElem){
			
			Element objectIdElem = (Element)elem.selectSingleNode("a:ObjectID");
			Element nameElem = (Element)elem.selectSingleNode("a:Name");
			Element codeElem = (Element)elem.selectSingleNode("a:Code");
			Element creatorElem = (Element)elem.selectSingleNode("a:Creator");
			Element creationDateElem = (Element)elem.selectSingleNode("a:CreationDate");
			Element modificationDateElem = (Element)elem.selectSingleNode("a:ModificationDate");
			Element modifierElem = (Element)elem.selectSingleNode("a:Modifier");
			Element commentElem = (Element)elem.selectSingleNode("a:Comment");

			Element dataTypeElem = (Element)elem.selectSingleNode("a:DataType");
			Element lengthElem = (Element)elem.selectSingleNode("a:Length");
			Element precisionElem = (Element)elem.selectSingleNode("a:Precision");

			PdmColumn column = new PdmColumn();
			column.setId(getLong(objectIdElem));
			column.setCode(getText(codeElem));
			column.setComment(getText(commentElem));
			column.setCreationDate(getText(creationDateElem));
			column.setCreator(getText(creatorElem));
			column.setName(getText(nameElem));
			column.setModificationDate(getText(modificationDateElem));
			column.setModifier(getText(modifierElem));
			column.setDataType(dataTypeElem.getText());
			column.setLength(getInt(lengthElem));
			column.setPrecision(
					getInt(precisionElem)
					);
			column.setPdmTableId(talbeId);
			columns.add(column);
		}
		
		
		return columns;
	}
	
	@Override
	public List<PdmKey> getKeys(Element tableElement) {
		// TODO Auto-generated method stub
		
		List<Element> keysElem = (List<Element>)tableElement.selectSingleNode("./c:Keys/o:key");
		for(Element elem : keysElem){
			
			/*		<c:Keys>
			<o:Key Id="o57">
			<a:ObjectID>0BA9EE39-D7F4-499F-8E4C-B4229F46251C</a:ObjectID>
			<a:Name>Key_1</a:Name>
			<a:Code>Key_1</a:Code>
			<a:CreationDate>1423642991</a:CreationDate>
			<a:Creator>Administrator</a:Creator>
			<a:ModificationDate>1423642991</a:ModificationDate>
			<a:Modifier>Administrator</a:Modifier>
			<c:Key.Columns>
			<o:Column Ref="o13"/>
			</c:Key.Columns>
			</o:Key>
			</c:Keys>*/
		}
		
		return null;
	}
	
	public PdmTable getTable(Document doc, String xpath){
		Element root = doc.getRootElement();
		Element elem = (Element)root.selectSingleNode(xpath);		
		return getTable(elem);
	}
	
	public PdmTable getTable(Element elem){
		PdmTable table = new PdmTable();
		
		Element objectIdElem = (Element)elem.selectSingleNode("a:ObjectID");
		Element nameElem = (Element)elem.selectSingleNode("a:Name");
		Element codeElem = (Element)elem.selectSingleNode("a:Code");
		Element creatorElem = (Element)elem.selectSingleNode("a:Creator");
		Element creationDateElem = (Element)elem.selectSingleNode("a:CreationDate");
		Element modificationDateElem = (Element)elem.selectSingleNode("a:ModificationDate");
		Element modifierElem = (Element)elem.selectSingleNode("a:Modifier");
		Element commentElem = (Element)elem.selectSingleNode("a:Comment");

		table.setId(getLong(objectIdElem));
		table.setCode(getText(codeElem));
		table.setComment(getText(commentElem));
		table.setCreationDate(getText(creationDateElem));
		table.setCreator(getText(creatorElem));
		table.setName(getText(nameElem));
		table.setModificationDate(getText(modificationDateElem));
		table.setModifier(getText(modifierElem));
		table.setColumn(this.getColumns(elem));
		return table;
	}
	
	/**
	 * 
	 */
	public List<PdmTable> getTables(Document doc) {
		
		Element root = doc.getRootElement();
		List<Element> tableElem = (List<Element>)root.selectNodes("//c:Tables/o:Table");
		List<PdmTable> tables = new ArrayList<PdmTable>(); 
		for(Element elem : tableElem){
			tables.add(this.getTable(elem));
		}
		return tables;
	}
	
	

}
